package wnhz.com.business.groupshop.ui;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import wnhz.com.business.R;
import wnhz.com.business.RegisterActivty;
import wnhz.com.business.base.BaseActivity;
import wnhz.com.business.impl.ActionBarClickListener;
import wnhz.com.business.uitls.ConfirmUtils;
import wnhz.com.business.uitls.CountDownTimerUtils;
import wnhz.com.business.uitls.FastUtils;
import wnhz.com.business.uitls.MyCallBack;
import wnhz.com.business.uitls.Url;
import wnhz.com.business.uitls.XUtil;
import wnhz.com.business.uitls.citychose.CityCodeChoseActivity;
import wnhz.com.business.view.TranslucentActionBar;

public class ResetBindOldPhoneActivity extends BaseActivity implements View.OnClickListener, ActionBarClickListener {

    @BindView(R.id.et_phone)
    TextView et_phone;
    @BindView(R.id.et_phone_code)
    EditText et_phone_code;
    @BindView(R.id.tv_post_code)
    TextView tv_post_code;
    @BindView(R.id.tv_next)
    TextView tv_next;
    @BindView(R.id.actionbar)
    TranslucentActionBar actionbar;
    @BindView(R.id.tv_chose_country)//选择国家区号
    TextView tv_chose_country;

    private String inputPhone, inputCode;
    private String phone;
    private int RESULT_GETCITYCODE = 200;
    private String code = "";
    private String countryname = "";

    public static Intent createIntent(Context context, String phone) {
        Intent intent = new Intent(context, ResetBindOldPhoneActivity.class);
        intent.putExtra("phone", phone);
        return intent;
    }

    public void parseIntent() {
        Intent intent = getIntent();
        phone = intent.getStringExtra("phone");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reset_bind_old_phone);
        ButterKnife.bind(this);
        parseIntent();
        actionbar.setData("验证手机号", R.drawable.ic_lefft_white, null, 0, null, 0, this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            actionbar.setStatusBarHeight(getStatusBarHeight());
        }
        et_phone.setText(phone);
        initView();
    }

    private void initView() {
        tv_chose_country.setOnClickListener(this);
        tv_post_code.setOnClickListener(this);
        tv_next.setOnClickListener(this);
        tv_next.setText("下一步");
    }


    @Override
    public void onClick(View v) {
        Intent intent = null;
        switch (v.getId()) {
            case R.id.tv_post_code:
                if (!FastUtils.isFastDoubleClick()) {
                    String inputPhone = et_phone.getText().toString();
                    if (!ConfirmUtils.isMobileNO(inputPhone)) {
                        Toast.makeText(this, "请输入有效手机号码", Toast.LENGTH_SHORT).show();
                    } else {
                        uploadYanZheng(inputPhone);
                        CountDownTimerUtils mCountDownTimerUtils = new CountDownTimerUtils(tv_post_code, 60000, 1000);
                        mCountDownTimerUtils.start();
                    }
                }
                break;
            case R.id.tv_next:
                if (!FastUtils.isFastDoubleClick()) {
                    checkInput();
                }
                break;
            case R.id.tv_chose_country:
                intent = new Intent(ResetBindOldPhoneActivity.this, CityCodeChoseActivity.class);
                startActivityForResult(intent, RESULT_GETCITYCODE);
                break;
            default:
                break;
        }
    }

    /**
     * 获取 验证码
     *
     * @param mobile
     */
    private void uploadYanZheng(String mobile) {
        Map<String, Object> map = new HashMap<>();
        map.put("mobile", mobile);
        map.put("type", 4);   //修改交易密码

        XUtil.Post(Url.HUOQUYANZHENGMA, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    if ("1".equals(jsonObject.optString("re"))) {

                    }
                    MyToast(jsonObject.optString("info"));

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                super.onFinished();
            }

        });
    }


    /**
     * 申请注册(验证输入)
     */
    private void checkInput() {
        inputPhone = et_phone.getText().toString();
        inputCode = et_phone_code.getText().toString();
        if (!ConfirmUtils.isMobileNO(inputPhone)) {
            Toast.makeText(this, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
        } else if (TextUtils.isEmpty(inputCode)) {
            Toast.makeText(this, "请输入验证码", Toast.LENGTH_SHORT).show();
        }

        yanZhengMaVeri(inputPhone, inputCode);

    }

    private void yanZhengMaVeri(final String mobile, final String code) {
        SharedPreferences sp = getSharedPreferences("user", Context.MODE_PRIVATE);
        String token = sp.getString("token", "");
        Map<String, Object> map = new HashMap<>();
        map.put("mobile", mobile);
        map.put("type", 4);  //验证验证码
        map.put("code", code);
        map.put("token", token);

        XUtil.Post(Url.USER_VERIFICATION, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    if ("1".equals(jsonObject.optString("re"))) {
                        //成功
                        startActivity(ResetBindNewPhoneActivity.createIntent(ResetBindOldPhoneActivity.this));
                        finish();
                    }
                    MyToast(jsonObject.optString("info"));

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                super.onFinished();
            }

        });
    }

    @Override
    public void onLeftClick() {
        finish();
    }

    @Override
    public void onRightClick() {

    }

    @Override
    public void onEditTextClick() {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == RESULT_GETCITYCODE && resultCode == 0) {
            if (data != null) {
                /**
                 * 获取国家地区的电话区号和国家名称
                 */
                code = data.getStringExtra("code");
                countryname = data.getStringExtra("area");
                tv_chose_country.setText(code+" "+countryname);
            }
        }
    }
}
